﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fcdbas.Core;
using Fcdbas.Core.Windows;
using Fcdbas.Platform;
using Fcdbas.WinUI.Common;
using Fcdbas.Platform.Properties;
using Fcdbas.Core.Reflection;
namespace Fcdbas.WinUI.Controls
{
    public class cDataSource : ISalWindow
    {
        public interface LateBind
        {
            SalBoolean vrtDataRecordCheckModify(IEntity entity);
            SalBoolean vrtDataRecordCheckNew(IEntity entity);
            SalBoolean vrtDataRecordCheckRemove(IEntity entity);
            SalBoolean vrtDataRecordCheckRequired();
            SalNumber vrtDataRecordCopy(SalNumber nWhat);
            SalNumber vrtDataRecordDuplicate(SalNumber nWhat);
            SalBoolean vrtDataRecordExecuteModify(IEntity entity);
            SalBoolean vrtDataRecordExecuteNew(IEntity entity);
            SalBoolean vrtDataRecordExecuteRemove(IEntity entity);
            SalBoolean vrtDataRecordExecuteStateEvent(IEntity entity, SalString sEvent);
            SalNumber vrtDataRecordFetchEditedUser(ref SalString lsAttr);
            SalBoolean vrtDataRecordGetDefaults();
            SalNumber vrtDataRecordNew(SalNumber nWhat);

            SalNumber vrtDataRecordPaste(SalNumber nWhat);
            SalBoolean vrtDataRecordPrepareNew(IEntity entity);
            SalNumber vrtDataRecordRemove(IEntity entity);
            SalNumber vrtDataRecordStateEvent(SalNumber nWhat, SalString sEvent);
            SalNumber vrtDataRecordToFormUser(ref SalString lsServerAttr, ref SalBoolean bMarkAsEdited);
            SalBoolean vrtDataRecordValidate();
            SalBoolean vrtDataSourceCheck(IEntity Entity);
            SalNumber vrtDataSourceClear(SalNumber nWhat, SalNumber nParam);

            SalNumber vrtDataSourceDetailModified(SalBoolean bDirty);
            SalBoolean vrtDataSourceExecuteMarkCommitted();
            SalBoolean vrtDataSourceExecuteMarkRollback();
            SalBoolean vrtDataSourceExecutePrepareRollback();
            SalBoolean vrtDataSourceExecuteSqlDelete(IEntity entity);
            SalBoolean vrtDataSourceExecuteSqlInsert(IEntity entity);
            SalBoolean vrtDataSourceExecuteSqlUpdate(IEntity entity);
            SalNumber vrtDataSourceFieldModified();

            SalNumber vrtDataSourceMarkRollback();
            SalNumber vrtDataSourcePopulate(SalNumber nWhat, SalNumber nParam);

            SalNumber vrtDataSourcePrepareRollback();

            SalNumber vrtDataSourceRefresh(DataSourcePopulate nWhat);
            SalNumber vrtDataSourceSave(SalNumber nWhat);
            SalBoolean vrtDataSourceSaveCheck(IEntity Entity);

            SalNumber vrtDataSourceSaveMarkCommitted();
            SalBoolean vrtDataSourceSaveModified(IEntity Entity);
            SalBoolean vrtDataSourceSaveNew(IEntity Entity);
            SalBoolean vrtDataSourceSaveRemoved(IEntity Entity);
            SalString vrtDataSourceUserOrderByGet();
            SalString vrtDataSourceUserWhereGet();
            SalString vrtDataSourceUserFieldGet();
            SalBoolean vrtDataSourceValidate();


        }

        private SalNumber I_nDataSourceState = 0;
        public SalString p_sLogicalUnit = "";
        public SalString p_sModule = "";
        public SalString p_sViewName = "";
        public SalString p_sPackageName = "";
        /// <summary>
        /// 默认的条件
        /// </summary>
        public SalString p_lsDefaultWhere = "";
        public SalString p_sDefaultOrderBy = "";
        public SalNumber p_nSourceFlags = 0;
        public SalBoolean dp_bWarnings = false;
        /// <summary>
        /// 父条件
        /// </summary>
        public SalString i_lsParentWhere = "";
        /// <summary>
        /// 用户查询条件
        /// </summary>
        public SalString i_lsUserWhere = "";
        public SalString i_lsDataTransferWhere = "";
        public SalString i_sUserOrderBy = "";
        /// <summary>
        /// 记录状态
        /// </summary>
        public SalNumber i_nRecordState = 0;
        //  public SalString __lsLastUserWhere = "";
        public SalString __lsLastUserOrderBy;
        private bool? customFieldsEnabled = null;
        public SalNumber __nDataDetailDirtyCount = 0;
        private IEntityList __DataSource;
        public static List<Biz.fnd_vpd_policy> VpdPolicys;
        public IEntityList DataSource
        {
            get { return __DataSource; }
        }
        public IEntityList CreateEntityList()
        {
            if (string.IsNullOrEmpty(this.LogicalObject) || this.LogicalObject == "..")
                return null;
            Type t = Fcdbas.Core.Reflection.TypeX.GetType(this.LogicalObject, true);
            if (t == null)
                return null;
            MethodInfoX method = MethodInfoX.Create(Entity.GetType(), "CreateEntityList");
            IEntityList list1 = method.Invoke(method) as IEntityList;
            return list1;
        }
        //public event WindowActionsEventHandler WindowActions;
        public IEntity Entity
        {
            get
            {
                DataSourceBinding dsb = this._derived as DataSourceBinding;
                if (dsb == null)
                {
                    Fcdbas.Core.Log.XTrace.WriteLine("Derived 的数据源为空！！！");
                    return null;
                }
                if (dsb.Current == null)
                {
                    if (string.IsNullOrEmpty(this.LogicalObject) || this.LogicalObject == "..")
                    {
                        Fcdbas.Core.Log.XTrace.WriteLine("业务逻辑对象为空！！！");
                        return null;
                    }
                    Type t = Fcdbas.Core.Reflection.TypeX.GetType(this.LogicalObject, true);
                    if (t == null)
                    {
                        Fcdbas.Core.Log.XTrace.WriteLine(string.Format("没有找到数据实体:{0}！！！", this.LogicalObject));
                        return null;
                    }
                    object obj = Fcdbas.Core.Reflection.TypeX.CreateInstance(t);
                    return obj as IEntity;

                }
                else
                {
                    IEntity entity = dsb.Current as IEntity;
                    //if (entity != null)
                    //{
                    //    if( entity["objid"] != null)
                    //        this.__sObjid = entity["objid"].ToString();
                    //    if(entity["objversion"] != null)
                    //        this.__lsObjversion = ((DateTime)entity["objversion"]).ToString("yyyy-MM-dd HH:mm:ss");
                    //}
                    return entity;
                }
                //return _cSessionManager.Entity;
            }
            //set
            //{
            //    _cSessionManager.Entity = value;
            //}
        }
        public delegate void DataRecordCheckModifyEventHandler(object sender, cDataSource.DataRecordCheckModifyEventArgs e);
        public delegate void DataRecordCheckNewEventHandler(object sender, cDataSource.DataRecordCheckNewEventArgs e);
        public delegate void DataRecordCheckRemoveEventHandler(object sender, cDataSource.DataRecordCheckRemoveEventArgs e);
        public delegate void DataRecordDuplicateEventHandler(object sender, cDataSource.DataRecordDuplicateEventArgs e);
        public delegate void DataRecordExecuteModifyEventHandler(object sender, cDataSource.DataRecordExecuteModifyEventArgs e);
        public delegate void DataRecordExecuteNewEventHandler(object sender, cDataSource.DataRecordExecuteNewEventArgs e);
        public delegate void DataRecordExecuteRemoveEventHandler(object sender, cDataSource.DataRecordExecuteRemoveEventArgs e);
        public delegate void DataRecordFetchEditedUserEventHandler(object sender, cDataSource.DataRecordFetchEditedUserEventArgs e);
        public delegate void DataRecordNewEventHandler(object sender, cDataSource.DataRecordNewEventArgs e);
        public delegate void DataRecordRemoveEventHandler(object sender, cDataSource.DataRecordRemoveEventArgs e);
        public delegate void DataRecordToFormUserEventHandler(object sender, cDataSource.DataRecordToFormUserEventArgs e);
        public delegate void DataSourceClearItEventHandler(object sender, cDataSource.DataSourceClearItEventArgs e);
        public delegate void DataRecordEditEventHandler(object sender, cDataSource.DataRecordEditEventArgs e);

        public delegate void DataSourceExecuteSqlCountEventHandler(object sender, cDataSource.DataSourceExecuteSqlCountEventArgs e);
        public delegate void DataSourceExecuteSqlSelectEventHandler(object sender, cDataSource.DataSourceExecuteSqlSelectEventArgs e);
        public delegate void DataSourceIsDirtyEventHandler(object sender, cDataSource.DataSourceIsDirtyEventArgs e);
        public delegate void DataSourceSaveEventHandler(object sender, cDataSource.DataSourceSaveEventArgs e);

        public delegate void DataRecordDeletedEventHandler(object sender, ReturnEventArgsSalBoolean e);
        public event EventHandler DataSourceSaveCompleted;
        public event DataRecordDeletedEventHandler DataSourceDeleted;
        protected cSessionManager _cSessionManager;
        public cSessionManager DbSesseion
        {
            get
            {
                return _cSessionManager;
            }
        }
        public SalBoolean IsNew
        {
            get
            {
                return (this.i_nRecordState & 40) == 8;
            }
        }
        public SalBoolean IsDelete
        {
            get
            {
                return (this.i_nRecordState & 32);
            }
        }
        public SalBoolean IsEdit
        {
            get
            {
                return ((this.i_nRecordState & 44) == 4);
            }
        }
        public SalNumber i_nDataSourceState
        {
            get
            {
                return this.I_nDataSourceState;
            }
            set
            {
                if (this.i_nDataSourceState != value)
                {
                    this.I_nDataSourceState = value;
                    //Sal.SendMsgToChildren(Sys.hWndForm, 16584, 0, 0);
                }
            }
        }
        public SalString __sObjstate
        {
            get
            {
                return this._cSessionManager.__sObjstate;
            }
            set
            {
                this._cSessionManager.__sObjstate = value;
            }
        }
        public SalString __lsAttr
        {
            get
            {
                return this._cSessionManager.__lsAttr;
            }
            set
            {
                this._cSessionManager.__lsAttr = value;
            }
        }

        public SalString __sObjevents
        {
            get
            {
                return this._cSessionManager.__sObjevents;
            }
            set
            {
                this._cSessionManager.__sObjevents = value;
            }
        }
        public SalString __lsResult
        {
            get
            {
                return this._cSessionManager.__lsResult;
            }
            set
            {
                this._cSessionManager.__lsResult = value;
            }
        }
        public SalString __sAction
        {
            get
            {
                return this._cSessionManager.__sAction;
            }
            set
            {
                this._cSessionManager.__sAction = value;
            }
        }
        public SalString __lsOldObjversion
        {
            get;
            set;
        }
        //public SalString __lsObjversion
        //{
        //    get
        //    {
        //        return this._cSessionManager.__lsObjversion;
        //    }
        //    set
        //    {
        //        this._cSessionManager.__lsObjversion = value;
        //    }
        //}

        //public SalString __sObjid
        //{
        //    get
        //    {
        //        return this._cSessionManager.__sObjid;
        //    }
        //    set
        //    {
        //        this._cSessionManager.__sObjid = value;
        //    }
        //}
        public ISalWindow _derived;
        //private System.Collections.Generic.List<System.Delegate> m_actionsHandlers = new System.Collections.Generic.List<System.Delegate>();
        public event WindowActionsEventHandler WindowActions
        {
            add
            {
                this.m_salWindow.AddWindowActions(value);
            }
            remove
            {
                this.m_salWindow.RemoveWindowActions(value);
            }
        }

        object ISalWindow.Derived
        {
            get
            {
                return this._derived;
            }
        }

        //public cDataSource()
        //{
        //    m_salWindow = new SalWindow(this);
        //    _cSessionManager = new cSessionManager(this);
        //}
        private SalWindow m_salWindow;
        public class ReturnEventArgsSalBoolean : ReturnEventArgs<SalBoolean>
        {
            public ReturnEventArgsSalBoolean()
            {
                returnValue = false;
            }
        }
        public class ReturnEventArgsSalNumber : ReturnEventArgs<SalNumber>
        {
            public ReturnEventArgsSalNumber()
            {
                returnValue = 0;
            }
        }
        public class ReturnEventArgsSalString : ReturnEventArgs<SalString>
        {
            public ReturnEventArgsSalString()
            {
                returnValue = "";
            }
        }
        /// <summary>
        /// 检查数据能否修改
        /// </summary>
        public class DataRecordCheckModifyEventArgs : ReturnEventArgs<SalBoolean>
        {
            private IEntity m_entity;
            public IEntity Entity
            {
                get
                {
                    return this.m_entity;
                }
            }
            public DataRecordCheckModifyEventArgs(IEntity entity)
            {
                this.m_entity = entity;
            }
            internal SalBoolean Return()
            {
                return this.returnValue;
            }
        }
        /// <summary>
        /// 检查数据能否更新
        /// </summary>
        public class DataRecordCheckNewEventArgs : ReturnEventArgs<SalBoolean>
        {
            private IEntity m_entity;
            public IEntity Entity
            {
                get
                {
                    return this.m_entity;
                }
            }
            public DataRecordCheckNewEventArgs(IEntity entity)
            {
                this.m_entity = entity;
            }
            internal SalBoolean Return()
            {
                return this.returnValue;
            }
        }
        /// <summary>
        /// 检查数据能否删除
        /// </summary>
        public class DataRecordCheckRemoveEventArgs : ReturnEventArgs<SalBoolean>
        {
            private IEntity m_entity;
            public IEntity Entity
            {
                get
                {
                    return this.m_entity;
                }
            }
            public DataRecordCheckRemoveEventArgs(IEntity Entity)
            {
                this.m_entity = Entity;
            }
            internal SalBoolean Return()
            {
                return this.returnValue;
            }
        }
        /// <summary>
        /// 数据复制
        /// </summary>
        public class DataRecordDuplicateEventArgs : ReturnEventArgs<SalNumber>
        {
            private SalNumber m_nWhat;
            public SalNumber nWhat
            {
                get
                {
                    return this.m_nWhat;
                }
            }
            public DataRecordDuplicateEventArgs(SalNumber nWhat)
            {
                this.m_nWhat = nWhat;
            }
            internal SalNumber Return()
            {
                return this.returnValue;
            }
        }
        /// <summary>
        /// 数据修改
        /// </summary>
        public class DataRecordExecuteModifyEventArgs : ReturnEventArgs<SalBoolean>
        {
            private IEntity m_entity;
            public IEntity Entity
            {
                get
                {
                    return this.m_entity;
                }
            }
            public DataRecordExecuteModifyEventArgs(IEntity Entity)
            {
                this.m_entity = Entity;
            }
            internal SalBoolean Return()
            {
                return this.returnValue;
            }
        }
        /// <summary>
        /// 新增数据
        /// </summary>
        public class DataRecordExecuteNewEventArgs : ReturnEventArgs<SalBoolean>
        {
            private IEntity m_entity;
            public IEntity Entity
            {
                get
                {
                    return this.m_entity;
                }
            }
            public DataRecordExecuteNewEventArgs(IEntity Entity)
            {
                this.m_entity = Entity;
            }
            internal SalBoolean Return()
            {
                return this.returnValue;
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        public class DataRecordExecuteRemoveEventArgs : ReturnEventArgs<SalBoolean>
        {
            private IEntity m_entity;
            public IEntity Entity
            {
                get
                {
                    return this.m_entity;
                }
            }
            public DataRecordExecuteRemoveEventArgs(IEntity Entity)
            {
                this.m_entity = Entity;
            }
            internal SalBoolean Return()
            {
                return this.returnValue;
            }
        }
        public class DataRecordFetchEditedUserEventArgs : ReturnEventArgs<SalNumber>
        {
            private SalString m_lsAttr;
            public SalString lsAttr
            {
                get
                {
                    return this.m_lsAttr;
                }
                set
                {
                    this.m_lsAttr = value;
                }
            }
            public DataRecordFetchEditedUserEventArgs(SalString lsAttr)
            {
                this.m_lsAttr = lsAttr;
            }
            public SalNumber Return(ref SalString lsAttr)
            {
                lsAttr = this.m_lsAttr;
                return this.returnValue;
            }
        }
        public class DataRecordNewEventArgs : ReturnEventArgs<SalNumber>
        {
            private SalNumber m_nWhat;
            public SalNumber nWhat
            {
                get
                {
                    return this.m_nWhat;
                }
            }
            public DataRecordNewEventArgs(SalNumber nWhat)
            {
                this.m_nWhat = nWhat;
            }
            internal SalNumber Return()
            {
                return this.returnValue;
            }
        }
        public class DataRecordEditEventArgs : ReturnEventArgs<SalNumber>
        {
            private SalNumber m_nWhat;
            public SalNumber nWhat
            {
                get
                {
                    return this.m_nWhat;
                }
            }
            public DataRecordEditEventArgs(SalNumber nWhat)
            {
                this.m_nWhat = nWhat;
            }
            internal SalNumber Return()
            {
                return this.returnValue;
            }
        }
        public class DataRecordRemoveEventArgs : ReturnEventArgs<SalNumber>
        {
            private IEntity m_nWhat;
            public IEntity nWhat
            {
                get
                {
                    return this.m_nWhat;
                }
            }
            public DataRecordRemoveEventArgs(IEntity nWhat)
            {
                this.m_nWhat = nWhat;
            }
            internal SalNumber Return()
            {
                return this.returnValue;
            }
        }

        public class DataSourceClearItEventArgs : ReturnEventArgs<SalBoolean>
        {
            private SalNumber m_nParam;
            public SalNumber nParam
            {
                get
                {
                    return this.m_nParam;
                }
            }
            public DataSourceClearItEventArgs(SalNumber nParam)
            {
                this.m_nParam = nParam;
            }
            internal SalBoolean Return()
            {
                return this.returnValue;
            }
        }

        public class DataSourceExecuteSqlCountEventArgs : ReturnEventArgs<SalNumber>
        {
            private SalString m_lsStmt;
            public SalString lsStmt
            {
                get
                {
                    return this.m_lsStmt;
                }
            }
            public DataSourceExecuteSqlCountEventArgs(SalString lsStmt)
            {
                this.m_lsStmt = lsStmt;
            }
            internal SalNumber Return()
            {
                return this.returnValue;
            }
        }
        public class DataSourceExecuteSqlSelectEventArgs : ReturnEventArgs<SalBoolean>
        {
            private SalString m_lsStmt;
            public SalString lsStmt
            {
                get
                {
                    return this.m_lsStmt;
                }
            }
            public DataSourceExecuteSqlSelectEventArgs(SalString lsStmt)
            {
                this.m_lsStmt = lsStmt;
            }
            internal SalBoolean Return()
            {
                return this.returnValue;
            }
        }
        public class DataSourceIsDirtyEventArgs : ReturnEventArgs<SalNumber>
        {
            private SalNumber m_nWhat;
            public SalNumber nWhat
            {
                get
                {
                    return this.m_nWhat;
                }
            }
            public DataSourceIsDirtyEventArgs(SalNumber nWhat)
            {
                this.m_nWhat = nWhat;
            }
            internal SalNumber Return()
            {
                return this.returnValue;
            }
        }

        public class DataSourceSaveEventArgs : ReturnEventArgs<SalNumber>
        {
            private SalNumber m_nWhat;
            public SalNumber nWhat
            {
                get
                {
                    return this.m_nWhat;
                }
            }
            public DataSourceSaveEventArgs(SalNumber nWhat)
            {
                this.m_nWhat = nWhat;
            }
            internal SalNumber Return()
            {
                return this.returnValue;
            }
        }
        public class DataRecordToFormUserEventArgs : ReturnEventArgs<SalNumber>
        {
            private SalString m_lsServerAttr;
            private SalBoolean m_bMarkAsEdited;
            public SalString lsServerAttr
            {
                get
                {
                    return this.m_lsServerAttr;
                }
                set
                {
                    this.m_lsServerAttr = value;
                }
            }
            public SalBoolean bMarkAsEdited
            {
                get
                {
                    return this.m_bMarkAsEdited;
                }
                set
                {
                    this.m_bMarkAsEdited = value;
                }
            }
            public DataRecordToFormUserEventArgs(SalString lsServerAttr, SalBoolean bMarkAsEdited)
            {
                this.m_lsServerAttr = lsServerAttr;
                this.m_bMarkAsEdited = bMarkAsEdited;
            }
            public SalNumber Return(ref SalString lsServerAttr, ref SalBoolean bMarkAsEdited)
            {
                lsServerAttr = this.m_lsServerAttr;
                bMarkAsEdited = this.m_bMarkAsEdited;
                return this.returnValue;
            }
        }
        //private IEntity CurrentEntity
        //{
        //    get {
        //        DataSourceBinding dsb = this._derived as DataSourceBinding;
        //        if (dsb == null)
        //            return null;
        //        return dsb.Current as IEntity;
        //    }
        //}
        //public cDataSource()
        //{
        //    WindowActions += new WindowActionsEventHandler(this.cDataSource_WindowActions);
        //}
        private bool _RecordRefresh = false;
        public bool RecordRefresh
        {
            get
            {
                return _RecordRefresh;
            }
            set
            {
                _RecordRefresh = value;
            }
        }
        public cDataSource(ISalWindow derived)
        {
            m_salWindow = new SalWindow(this);
            _cSessionManager = new cSessionManager(this);
            this._derived = derived;
            //this.InitializeMultipleInheritance();
            WindowActions += new WindowActionsEventHandler(this.cDataSource_WindowActions);
            this._derived.AttachMessageActions(this);
        }
        private void cDataSource_WindowActions(object sender, WindowActionsEventArgs e)
        {
            //暂时注释，需要响应不用的消息
            if (e.ActionType == Const.PM_DataRecordRefresh)
                return;
            if (e.ActionType != Const.PM_DataSourceSave)
                if (Fcdbas.Core.Util.StringUtil.IsNumber(e.LParam.ToString()) && int.Parse(e.LParam.ToString()) != this.DataSourceBind.Identify)
                    return;
            int actionType = e.ActionType;
            if (actionType <= Const.AM_DataSourceFieldModified)
            {
                if (actionType <= 16412)
                {

                }
                else
                {
                    if (actionType <= 16539)
                    {

                    }
                    else
                    {

                        switch (actionType)
                        {

                            case Const.PM_DataSourceClear:
                                e.Handled = true;
                                e.Return = this.vrtDataSourceClear(int.Parse(Sys.wParam.ToString()), int.Parse(Sys.lParam.ToString()));
                                return;
                            case Const.PM_DataSourceSave:
                                //e.Handled = true;
                                // e.Return = 
                                if (this.vrtDataSourceSave(int.Parse(Sys.wParam.ToString())) == 0)
                                {
                                    e.Handled = true;
                                    e.Return = 0;
                                }
                                return;

                            case Const.PM_DataSourceIsDirty:
                                e.Handled = true;
                                // e.Return = this.vrtDataSourceIsDirty((SalNumber)Sys.wParam);
                                return;

                            case Const.AM_DataSourceValidate:
                                e.Handled = true;
                                e.Return = this.vrtDataSourceValidate();
                                return;

                            case Const.PM_DataRecordNew:
                                // e.Handled = false;

                                this.vrtDataRecordNew(int.Parse(Sys.wParam.ToString()));
                                //  e.Return = 0;
                                return;

                            case Const.PM_DataRecordRemove:
                                e.Handled = true;
                                if (Sys.wParam is IEntity)
                                    e.Return = this.vrtDataRecordRemove(Sys.wParam as IEntity);
                                else
                                    e.Return = this.DataRecordRemoveMulti(Sys.wParam as List<IEntity>);
                                return;
                            case Const.PM_DataRecordDuplicate:
                                e.Handled = true;
                                e.Return = this.vrtDataRecordDuplicate(int.Parse(Sys.wParam.ToString()));
                                return;


                            default:
                                switch (actionType)
                                {
                                    case Const.AM_DataSourceDetailModified:
                                        e.Handled = true;
                                        e.Return = this.vrtDataSourceDetailModified(int.Parse(Sys.wParam.ToString()));
                                        return;
                                    case Const.AM_DataSourceFieldModified:
                                        e.Handled = false;
                                        //e.Return =false;
                                        this.vrtDataSourceFieldModified();
                                        return;
                                    default:
                                        return;
                                }
                                break;
                        }
                    }
                }
            }
            else
            {
                if (actionType <= Const.AM_UserDefinedQueryRemove)
                {
                    if (actionType <= Const.AM_DataSourceReferenceItems)
                    {
                        switch (actionType)
                        {
                            case Const.AM_DataSourcePrepareRollback:
                                e.Handled = true;
                                e.Return = this.vrtDataSourcePrepareRollback();
                                return;
                            case Const.AM_DataSourceMarkRollback:
                                e.Handled = true;
                                e.Return = this.vrtDataSourceMarkRollback();
                                return;

                            default:
                                if (actionType != 16746)
                                {
                                    return;
                                }

                                return;
                        }
                    }
                    else
                    {
                        switch (actionType)
                        {

                            //case Const.AM_DataSourceSaveCheck:
                            //    e.Handled = true;
                            //    e.Return = this.vrtDataSourceSaveCheck();
                            //    return;
                            //case Const.AM_DataSourceSaveNew:
                            //    this.cDataSource_OnAM_DataSourceSaveNew(sender, e);
                            //    return;
                            //case Const.AM_DataSourceSaveModified:
                            //    this.cDataSource_OnAM_DataSourceSaveModified(sender, e);
                            //    return;
                            //case Const.AM_DataSourceSaveRemoved:
                            //    this.cDataSource_OnAM_DataSourceSaveRemoved(sender, e);
                            //    return;
                            case Const.AM_DataSourceSaveMarkCommitted:
                                e.Handled = true;
                                e.Return = this.vrtDataSourceSaveMarkCommitted();
                                return;


                            case Const.PM_DataSourceRefresh:
                                e.Handled = true;
                                e.Return = this.vrtDataSourceRefresh((DataSourcePopulate)(Sys.wParam));
                                return;

                            default:

                                break;
                        }
                    }
                }
                else
                {

                    {
                        switch (actionType)
                        {


                            case Const.PM_DataRecordCopy:
                                e.Handled = true;
                                e.Return = this.vrtDataRecordCopy((SalNumber)Sys.wParam);
                                return;
                            case Const.PM_DataRecordPaste:
                                e.Handled = true;
                                e.Return = this.vrtDataRecordPaste((SalNumber)Sys.wParam);
                                return;

                            default:

                                break;
                        }
                    }
                }
            }
            e.Handled = true;
            e.Return = this.vrtDataSourcePopulate((SalNumber)Sys.wParam, (SalNumber)Sys.lParam);
        }


        public virtual SalBoolean vrtDataSourceSaveNew(IEntity Entitiy)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceSaveNew(Entitiy);
            }
            return lateBind.vrtDataSourceSaveNew(Entitiy);
        }
        public virtual SalBoolean vrtDataSourceSaveModified(IEntity Entitiy)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceSaveModified(Entitiy);
            }
            return lateBind.vrtDataSourceSaveModified(Entitiy);
        }


        public SalNumber DataSourceSave(SalNumber nWhat)
        {
            SalString salString = "";
            switch (nWhat)
            {
                case 1:
                    SalWindow.WaitCursor(true);
                    SalBoolean result = true;
                    IEntity Entity = null;
                    Committed = true;
                    if (NeedRollback)
                        return 0;
                    if (this.DataSourceBind.List.Count <= 0)
                    {
                        if (this.DataSourceSaveCompleted != null)
                        {
                            this.DataSourceSaveCompleted(this, EventArgs.Empty);
                        }
                        return 1;
                    }
                    Entity = this.DataSourceBind.List[0] as IEntity;
                    //需要增加校验函数
                    //if (_cSessionManager.DbTransactionBegin(Entity))
                    {
                        for (int i = 0; i < this.DataSourceBind.List.Count; i++)
                        {
                            Entity = this.DataSourceBind.List[i] as IEntity;
                            if (Entity == null || Entity.Status == RecordStatus.Normal)
                                continue;
                            if (this.vrtDataSourceSaveCheck(Entity))
                            {

                                if (this.vrtDataSourceSaveRemoved(Entity))
                                {
                                    this.DataSourceExecutePrepareRollback();
                                    if (this.vrtDataSourceSaveModified(Entity) && this.vrtDataSourceSaveNew(Entity))
                                    {
                                        //if (Entity.Status == RecordStatus.Delete)
                                        //{
                                        //    this.DataSourceBind.List.RemoveAt(i);
                                        //    i--;
                                        //}
                                        result = true;
                                        // result = !( this.i_nDataSourceState == 2);
                                        // return 1;
                                    }
                                    else
                                    {
                                        result = false;
                                        break;
                                    }

                                }
                                else
                                {

                                    result = false;
                                    break;
                                }
                            }
                            else
                            {
                                result = false;
                                break;
                            }
                        }
                    }
                    if (result)
                    {
                        //_cSessionManager.DbTransactionEnd(Entity);
                        //for (int i = 0; i < this.DataSourceBind.List.Count; i++)
                        //{
                        //    Entity = this.DataSourceBind.List[i] as IEntity;
                        //    if (Entity == null || Entity.Status == RecordStatus.Normal)
                        //        continue;

                        //    if (Entity.Status == RecordStatus.Delete)
                        //    {
                        //        this.DataSourceBind.List.RemoveAt(i);
                        //        i--;
                        //    }
                        //    Entity.Status = RecordStatus.Normal;
                        //}
                        this.vrtDataSourceSaveMarkCommitted();
                        // base.MethodProgressStep();
                        if (this.DataSourceSaveCompleted != null)
                        {
                            this.DataSourceSaveCompleted(this, EventArgs.Empty);
                        }
                    }
                    else
                    {
                        //_cSessionManager.DbTransactionClear(Entity);

                        this.vrtDataSourceMarkRollback();
                        if (this.DataSourceSaveCompleted != null)
                        {
                            this.DataSourceSaveCompleted(this, EventArgs.Empty);
                        }
                        return 0;
                    }
                    SalWindow.WaitCursor(false);
                    break;
                case 2:
                    //if (this.DraftMode)
                    //{
                    //    return true;
                    //}
                    return this.i_nDataSourceState == 2 || this.i_nDataSourceState == 3;
                case 3:
                    break;
                default:
                    return 0;
            }
            return 4;
        }

        public SalNumber DataRecordCopy(SalNumber nWhat)
        {
            //cMessage cMessage = new cMessage();
            switch (nWhat)
            {
                case 1:
                    //cMessage.SetName("IFS.COPYOBJECT");
                    //cMessage.AddAttribute("LU", this.p_sLogicalUnit);
                    //cMessage.AddAttribute("VIEW", Var.Security._RemoveDbPrefix(this.p_sViewName));
                    //cMessage.AddAttribute("RECORD", this._DataRecordStoreMsg());
                    //Int._PalSetClipboardData(cMessage.Pack());
                    return 1;
                case 2:
                    return this.i_nDataSourceState != 0;
                case 3:
                    return 4;
                default:
                    return 0;
            }
        }
        /// <summary>
        /// 数据复制，未实现
        /// </summary>
        /// <param name="nWhat"></param>
        /// <returns></returns>
        public SalNumber DataRecordDuplicate(SalNumber nWhat)
        {
            //SalArray<SalString> sStoredValues = new SalArray<SalString>();
            //SalNumber salNumber = 0;
            //SalArray<SalNumber> nItemIndex = new SalArray<SalNumber>();
            switch (nWhat)
            {
                case 1:
                    //if (!Sal.SendMsg(base.i_hWndSelf, 16699, 1, 0))
                    //{
                    //    return 0;
                    //}
                    //salNumber = this._DataRecordStore(sStoredValues, nItemIndex);
                    SalWindow.WaitCursor(true);
                    this.i_nRecordState = 8;
                    //base.SendMessageToChildren(64, 16786, 1, 1);
                    //this.__DataRecordEnforceKeysGet(true);
                    //using (FndSqlDependent.SuspendLookup(base.i_hWndFrame))
                    //{
                    //    this.vrtDataRecordGetDefaults();
                    //    if (!(this.p_nSourceFlags & 2))
                    //    {
                    //        this.vrtDataRecordPrepareNew();
                    //        this.DataRecordPrepareNewCustomFields();
                    //    }
                    //    base.SendMessageToChildren(16, 16772, 1, 0);
                    //    if (salNumber > 0)
                    //    {
                    //        this._DataRecordRestore(sStoredValues, nItemIndex, salNumber);
                    //    }
                    //}
                    //this.vrt_DataSourceFocusOnFirst();
                    //this._DataSourceStateSetDirty(true, 0);
                    //this.__AddNavigationHistory(16705, "");
                    SalWindow.WaitCursor(false);
                    return 1;
                case 2:
                // return Sal.SendMsg(base.i_hWndSelf, 16705, 2, 0) && this.i_nDataSourceState != 0;
                case 3:
                    return 4;
                default:
                    return 0;
            }
        }
        public SalBoolean DataRecordGetDefaults()
        {
            return true;
        }
        public SalNumber DataRecordNew(SalNumber nWhat)
        {
            switch (nWhat)
            {
                case 1:
                    {

                        SalWindow.WaitCursor(true);
                        this.i_nRecordState = 8;
                        NeedRollback = false;

                        this.Committed = false;
                        if (!(this.p_nSourceFlags & 2))
                        {
                            this.vrtDataRecordPrepareNew(this.Entity);
                            this.DataRecordPrepareNewCustomFields();
                        }


                        SalWindow.WaitCursor(false);
                        return 1;
                    }
                case 2:
                    SalWindow.WaitCursor(true);
                    this.i_nRecordState = 8;
                    NeedRollback = false;

                    this.Committed = false;



                    SalWindow.WaitCursor(false);
                    return 1;
                case 3:
                    return 4;
                default:
                    return 0;
            }
        }

        public SalNumber DataRecordPaste(SalNumber nWhat)
        {
            //cMessage cMessage = new cMessage();
            SalString sMessage = "";
            switch (nWhat)
            {
                case 1:
                    if (!this.DataRecordPaste(2))
                    {
                        return 0;
                    }
                    //cMessage.Unpack(Int._PalGetClipboardData());
                    //if (!Sal.SendMsg(base.i_hWndSelf, 16699, 1, 0))
                    //{
                    //    return 0;
                    //}
                    SalWindow.WaitCursor(true);
                    this.i_nRecordState = 8;
                    //base.SendMessageToChildren(64, 16786, 1, 1);
                    //this.__DataRecordEnforceKeysGet(true);
                    //this.vrtDataRecordGetDefaults();
                    if (!(this.p_nSourceFlags & 2))
                    {
                        this.vrtDataRecordPrepareNew(this.Entity);
                        this.DataRecordPrepareNewCustomFields();
                    }
                    //base.SendMessageToChildren(16, 16772, 1, 0);
                    //this._DataRecordRestoreMsg(cMessage.FindAttribute("RECORD", ""));
                    //this.vrt_DataSourceFocusOnFirst();
                    //this._DataSourceStateSetDirty(true, 0);
                    SalWindow.WaitCursor(false);
                    return 1;
                case 2:
                    //if (Sal.SendMsg(base.i_hWndSelf, 16705, 2, 0))
                    //{
                    //    sMessage = Int._PalGetClipboardData();
                    //    if (cMessage.Unpack(sMessage))
                    //    {
                    //        return cMessage.Name == "IFS.COPYOBJECT" && cMessage.FindAttribute("LU", "@") == this.p_sLogicalUnit && cMessage.FindAttribute("VIEW", "@") == Var.Security._RemoveDbPrefix(this.p_sViewName);
                    //    }
                    //}
                    return 0;
                case 3:
                    return 4;
                default:
                    return 0;
            }
        }
        public SalBoolean DataRecordPrepareNew(IEntity Entity)
        {
            if (!(this.p_sPackageName != ""))
            {
                return false;
            }
            if (this.p_sPackageName.ToString().IndexOf('(') >= 0)
                return true;
            if (Entity == null)
                return false;
            SalString sSql = this.p_sPackageName + ".NEW__( :info_, :objid_, :objversion_, :attr_, :action_ )";
            if (cSessionManager.IsRemote)
            {
                cSessionManager.PrepareSQLBlock(Entity, sSql, true, true, null, System.Data.CommandType.StoredProcedure, 0, "PREPARE");
                DataContract.ExecuteResult result = cSessionManager.BatchExecute();
                if (result != null && result.Success)
                {

                    if (this.DataSourceBind != null)
                        this.DataSourceBind.ResetCurrentItem();
                    return true;
                }
                else
                {
                    cSessionManager.translate.ShowMessage(cSessionManager.GetSqlLastError(), "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return false;
                }
            }
            else
            {
                this.__lsAttr = Entity.GetAttr(false);
                //this.DataRecordFetchEdited(ref this.__lsAttr);
                //cSessionManager.DbTransactionBegin();
                this._cSessionManager._SetAction("PREPARE");
                if (!_cSessionManager.DbSQLBlock(Entity, sSql))
                {
                    Entity.EditAttr("");
                    Entity.EditDiffAttr("");
                    //  cSessionManager.DbTransactionClear();
                    return false;
                }
                // cSessionManager.DbTransactionClear();
                if (_cSessionManager.HandleSqlResult(this.__lsResult))
                {
                    //this.__lsAttr = Entity.GetAttr();
                    Entity.EditAttr("");
                    Entity.EditDiffAttr("");
                    this.DataRecordToForm(Entity, this.__lsAttr, true);
                    return true;
                }
                return false;
            }
        }
        public SalNumber DataRecordRemove(IEntity entity)
        {
            //if (entity.Status == RecordStatus.New)
            //{
            //    this.DataSourceBind.RemoveCurrent();
            //    i_nRecordState = 0;
            //    return 1;
            //}
            bool newDelete = false;
            if (entity.Status == RecordStatus.Delete || entity.Status == RecordStatus.New)
            {
                if (entity.Status == RecordStatus.New)
                {
                    newDelete = true;
                    this.DataSourceBind.RemoveCurrent();
                    if (DataSourceBind.Count > 0)
                        this.DataSourceBind.Position = 0;
                    else
                    {
                        this.DataSourceBind.Position = -1;
                    }
                }
                else
                {
                    entity.Status = RecordStatus.Normal;
                }

                IEntityList list = this.DataSourceBind.DataSource as IEntityList;
                if (list != null)
                {
                    bool isDel = false;
                    foreach (IEntity en in list)
                    {
                        if (en.Status == RecordStatus.Delete)
                        {
                            isDel = true;
                            i_nRecordState = 32;
                            break;
                        }
                        if (en.Status == RecordStatus.New)
                        {
                            isDel = true;
                            i_nRecordState = 8;
                            break;
                        }
                        if (en.Status == RecordStatus.Edit)
                        {
                            isDel = true;
                            i_nRecordState = 4;
                            break;
                        }

                    }
                    if (isDel)
                    {
                        this.Committed = false;

                    }
                    else
                    {
                        i_nRecordState = 0;
                        this.Committed = true;
                    }
                }
                else
                {
                    i_nRecordState = 0;
                    this.Committed = true;
                }
                if (DataSourceDeleted != null)
                {
                    ReturnEventArgsSalBoolean re = new ReturnEventArgsSalBoolean();
                    re.ReturnValue = newDelete;
                    DataSourceDeleted(this, re);
                }
                return 1;
            }
            if (this.vrtDataRecordCheckRemove(entity))
            {
                this.Committed = false;
                i_nRecordState = 32;
                entity.Status = RecordStatus.Delete;
                if (DataSourceDeleted != null)
                {
                    ReturnEventArgsSalBoolean re = new ReturnEventArgsSalBoolean();
                    re.ReturnValue = newDelete;
                    DataSourceDeleted(this, re);
                }
                return 1;
            }
            else
                return 0;

        }
        public SalNumber _DataSourceStateSetDirty(SalBoolean bSet, SalNumber nState)
        {

            SalNumber x = 0;
            if (bSet)
            {
                if (this.i_nDataSourceState != 2)
                {
                    x = this.i_nDataSourceState;
                    this.i_nDataSourceState = 2;
                    this.Entity.Status = RecordStatus.Delete;
                    // base.MethodStateChanged(4);
                }
            }
            else
            {
                if (this.i_nDataSourceState == 2 || this._DataSourceIsDetailModified())
                {
                    if (this._DataSourceIsDetailModified())
                    {
                        nState = 3;
                    }
                    if (this.i_nDataSourceState != nState)
                    {
                        this.i_nDataSourceState = nState;
                        // base.SendMessageToParent(16728, 0, 0);
                        // base.MethodStateChanged(4);
                    }
                }
                else
                {
                    if (this.i_nDataSourceState != 3 && this.i_nDataSourceState != nState)
                    {
                        this.i_nDataSourceState = nState;
                        // base.MethodStateChanged(4);
                    }
                }
            }
            return 0;
        }
        public SalBoolean _DataSourceIsDetailModified()
        {
            return this.__nDataDetailDirtyCount != 0;
        }

        public SalString DataRecordIdGet()
        {
            SalString result = "";
            //if (this.__hWndObjid != SalWindowHandle.Null)
            //{
            //    Sal.GetWindowText(this.__hWndObjid, ref result, 32767);
            //    return result;
            //}
            return "";// Vis.StrChoose(this.i_nDataSourceState != 0 && !(this.i_nRecordState & 8), this.__sObjid, "");
        }
        public SalString DataRecordVersionGet()
        {
            SalString result = "";
            //if (this.__hWndObjversion != SalWindowHandle.Null)
            //{
            //    Sal.GetWindowText(this.__hWndObjversion, ref result, 32767);
            //    return result;
            //}
            return "";// Vis.StrChoose(this.i_nDataSourceState != 0 && !(this.i_nRecordState & 8), this.__lsObjversion, "");
        }
        public SalString DataRecordStateGet()
        {
            SalString result = "";
            //if (this.__hWndObjstate != SalWindowHandle.Null)
            //{
            //    Sal.GetWindowText(this.__hWndObjstate, ref result, 32767);
            //    return result;
            //}
            return Vis.StrChoose(this.i_nDataSourceState != 0 && !(this.i_nRecordState & 8), this.__sObjstate, "");
        }
        public SalString DataRecordEventsGet()
        {
            SalString result = "";
            //if (this.__hWndObjevents != SalWindowHandle.Null)
            //{
            //    Sal.GetWindowText(this.__hWndObjevents, ref result, 32767);
            //    return result;
            //}
            return Vis.StrChoose(this.i_nDataSourceState != 0 && !(this.i_nRecordState & 8), this.__sObjevents, "");
        }
        public SalNumber DataRecordToForm(IEntity Entity, SalString sAttr, SalBoolean bMarkAsEdited)
        {
            Entity.SetAttr(sAttr, true);
            if (this.DataSourceBind != null)
                this.DataSourceBind.ResetCurrentItem();

            return 0;
        }

        internal void CustomFieldsEnabledReset()
        {
            this.customFieldsEnabled = null;
        }
        internal bool CustomFieldsEnabled
        {
            get
            {
                //if (!this.customFieldsEnabled.HasValue)
                //{
                //    this.customFieldsEnabled = new bool?(CustomObjects.Fields.CountActive(this) > 0);
                //}
                return this.customFieldsEnabled.Value;
            }
        }
        internal bool DataRecordPrepareNewCustomFields()
        {
            //if (this.CustomFieldsEnabled)
            //{
            //    string text = CustomObjects.Fields.FindPackageName(this);
            //    if (text == string.Empty)
            //    {
            //        return true;
            //    }
            //    base.__lsAttr = this.OrgAttr;
            //    SalArray<SalString> arg_45_0 = Var.__g_Bind.s;
            //    int[] indices = new int[1];
            //    arg_45_0[indices] = this.CFAttr;
            //    base._SetAction("PREPARE");
            //    if (!base.DbPLSQLBlock(cSessionManager.c_hSql, string.Format("&AO.{0}.Cf_New__(:info_, :objid_, :__g_Bind.s[0], :attr_, :action_ )", text)))
            //    {
            //        return false;
            //    }
            //    base.StoreSqlResult(base.__lsResult);
            //    SalArray<SalString> arg_9F_0 = Var.__g_Bind.s;
            //    int[] indices2 = new int[1];
            //    this.DataRecordToForm(arg_9F_0[indices2], true);
            //}
            return true;
        }
        internal bool DataRecordExecuteNewCustomFields(IEntity entity)
        {
            //if (this.CustomFieldsEnabled && !this.CFAttr.IsEmpty)
            //{
            //    base.__lsAttr = this.OrgAttr;
            //    SalArray<SalString> arg_3F_0 = Var.__g_Bind.s;
            //    int[] indices = new int[1];
            //    arg_3F_0[indices] = this.CFAttr;
            //    base._SetAction("DO");
            //    if (!base.DbPLSQLBlock(hSql, string.Format("&AO.{0}.Cf_Modify__(:info_, :objid_, :__g_Bind.s[0], :attr_, :action_ )", CustomObjects.Fields.FindPackageName(this))))
            //    {
            //        return false;
            //    }
            //    base.StoreSqlResult(base.__lsResult);
            //    SalArray<SalString> arg_9A_0 = Var.__g_Bind.s;
            //    int[] indices2 = new int[1];
            //    this.DataRecordToForm(arg_9A_0[indices2], false);
            //}
            return true;
        }
        internal bool DataRecordExecuteModifyCustomFields(IEntity entity)
        {
            //if (this.CustomFieldsEnabled && !this.CFAttr.IsEmpty)
            //{
            //    base.__lsAttr = this.OrgAttr;
            //    SalArray<SalString> arg_3F_0 = Var.__g_Bind.s;
            //    int[] indices = new int[1];
            //    arg_3F_0[indices] = this.CFAttr;
            //    base._SetAction("DO");
            //    if (!base.DbPLSQLBlock(hSql, string.Format("&AO.{0}.Cf_Modify__(:info_, :objid_, :__g_Bind.s[0], :attr_, :action_ )", CustomObjects.Fields.FindPackageName(this))))
            //    {
            //        return false;
            //    }
            //    base.StoreSqlResult(base.__lsResult);
            //    SalArray<SalString> arg_9A_0 = Var.__g_Bind.s;
            //    int[] indices2 = new int[1];
            //    this.DataRecordToForm(arg_9A_0[indices2], false);
            //}
            return true;
        }

        //public SalBoolean DataSourceExecuteSqlKeyList(SalWindowHandle hWndKeyList, SalString lsStmt)
        //{
        //    cDataSource.SqlLocals.DataSourceExecuteSqlKeyListLocals dataSourceExecuteSqlKeyListLocals = new cDataSource.SqlLocals.DataSourceExecuteSqlKeyListLocals();
        //    SalBoolean result;
        //    using (new SqlContext(dataSourceExecuteSqlKeyListLocals, this))
        //    {
        //        dataSourceExecuteSqlKeyListLocals.lsStmt = lsStmt;
        //        Int._ObsoleteWarning(1, "DataRecordExecuteSqlKeyList");
        //        Sal.ListClear(hWndKeyList);
        //        if (this.i_nDataSourceMaxKeys == 0)
        //        {
        //            this.i_nDataSourceMaxKeys = 100;
        //        }
        //        base.DbPrepareAndExecute(cSessionManager.c_hSql, dataSourceExecuteSqlKeyListLocals.lsStmt + " INTO :sText");
        //        dataSourceExecuteSqlKeyListLocals.nFetch = 0;
        //        using (new WhenSqlError())
        //        {
        //            try
        //            {
        //                while (cSessionManager.c_hSql.FetchNext(ref dataSourceExecuteSqlKeyListLocals.nFetch) && dataSourceExecuteSqlKeyListLocals.nFetch == 0 && !dataSourceExecuteSqlKeyListLocals.bWarning)
        //                {
        //                    dataSourceExecuteSqlKeyListLocals.nCounter += 1;
        //                    Sal.ListAdd(hWndKeyList, dataSourceExecuteSqlKeyListLocals.sText);
        //                    if (dataSourceExecuteSqlKeyListLocals.nCounter > this.i_nDataSourceMaxKeys)
        //                    {
        //                        dataSourceExecuteSqlKeyListLocals.bWarning = true;
        //                        SalArray<SalString> arg_EA_0 = dataSourceExecuteSqlKeyListLocals.sParam;
        //                        int[] indices = new int[1];
        //                        arg_EA_0[indices] = this.i_nDataSourceMaxKeys.ToString(0);
        //                        dataSourceExecuteSqlKeyListLocals.nReply = Int.AlertBoxWithParams(Resources.TEXT_DataSourceManyKeys, Resources.CAPTION_Warning, 52, dataSourceExecuteSqlKeyListLocals.sParam);
        //                    }
        //                }
        //                if (dataSourceExecuteSqlKeyListLocals.bWarning && dataSourceExecuteSqlKeyListLocals.nReply == 6)
        //                {
        //                    Sal.ListClear(hWndKeyList);
        //                    base.DbListPopulate(hWndKeyList, cSessionManager.c_hSql, dataSourceExecuteSqlKeyListLocals.lsStmt);
        //                }
        //                result = true;
        //            }
        //            catch (SalSqlError error)
        //            {
        //                base._HandleSqlError(error);
        //                result = false;
        //            }
        //        }
        //    }
        //    return result;
        //}
        //public SalBoolean DataSourceExecuteSqlRecordList(SalWindowHandle hWnd, SalString lsStmt)
        //{
        //    if (lsStmt != "" && (!base.DbPrepare(cSessionManager.c_hSql, lsStmt) || !base.DbExecute(cSessionManager.c_hSql)))
        //    {
        //        return false;
        //    }
        //    return Sal.SendMsg(hWnd, 16744, lsStmt != "", this.i_nDataSourceMaxKeys);
        //}
        //public SalNumber DataSourceExecuteSqlCount(SalString lsStmt)
        //{
        //    SalNumber x = 0;
        //    if (base.DbPrepare(cSessionManager.c_hSql, lsStmt) && base.DbExecute(cSessionManager.c_hSql) && base.DbFetchNext(cSessionManager.c_hSql, ref x) && x == 0)
        //    {
        //        SalArray<SalNumber> arg_5B_0 = Var.__g_Bind.n;
        //        int[] indices = new int[1];
        //        return arg_5B_0[indices];
        //    }
        //    return -1;
        //}
        public SalBoolean DataSourceValidate()
        {
            if ((SalBoolean)(this.i_nDataSourceState & 2) && !this.vrtDataRecordValidate())
            {
                return false;
            }
            return true;
            // return base.SendMessageToChildrenOk(16, 16703, 0, 0);
        }
        public SalBoolean DataSourceSaveCheck(IEntity entity)
        {
            if (!this.vrtDataSourceCheck(entity))
            {
                return false;
            }
            return true;

        }

        public SalBoolean DataSourceSaveNew(IEntity entity)
        {
            if (!this.vrtDataSourceExecuteSqlInsert(entity))
            {
                return false;
            }
            return true;
        }
        public SalBoolean DataSourceSaveModified(IEntity entity)
        {
            if (!this.vrtDataSourceExecuteSqlUpdate(entity))
            {
                return false;
            }
            return true;
        }
        public SalBoolean DataSourceSaveRemoved(IEntity entity)
        {
            SalNumber x = 0;

            x = this.vrtDataSourceExecuteSqlDelete(entity);

            return x;
        }
        public SalNumber DataSourcePrepareRollback()
        {
            if (!this.vrtDataSourceExecutePrepareRollback())
            {
                return false;
            }
            return true;
        }
        public SalNumber DataSourceSaveMarkCommitted()
        {
            int num = this.i_nRecordState;
            this.vrtDataSourceExecuteMarkCommitted();
            // base.SendMessageToChildren(16, 16769, 0, 0);

            return 0;
        }
        public SalNumber DataSourceMarkRollback()
        {
            if (!this.vrtDataSourceExecuteMarkRollback())
            {
                return false;
            }
            return true;

        }
        public SalBoolean DataSourceCheck(IEntity Entity)
        {
            //if (!(!(this.p_nSourceFlags & 2) && this.p_bWarnings))
            //{
            //    return true;
            //}
            if (Entity.Status == RecordStatus.New)
            {
                return this.vrtDataRecordCheckNew(Entity);
            }
            if (Entity.Status == RecordStatus.Edit)
            {
                return this.vrtDataRecordCheckModify(Entity);
            }
            return true;
        }
        public SalBoolean DataSourceExecutePrepareRollback()
        {
            //this.__lsOldObjversion = this.__lsObjversion;
            return true;
        }
        /// <summary>
        /// 清除数据源，未实现
        /// </summary>
        /// <param name="nWhat"></param>
        /// <param name="nParam"></param>
        /// <returns></returns>
        public SalNumber DataSourceClear(SalNumber nWhat, SalNumber nParam)
        {
            switch (nWhat)
            {
                case 1:

                    return 1;
                case 2:
                    return this.i_nDataSourceState != 0;
                case 3:
                    return 4;
                default:
                    return 0;
            }
        }

        public SalBoolean DataSourceExecuteSqlInsert(IEntity Entity)
        {
            if (Entity.Status == RecordStatus.New)
            {
                //return DataRecordExecuteNew(Entity);
                return vrtDataRecordExecuteNew(Entity);
                //base.SendMessageToChildrenOk(4096, 17504, 1, 0) && this.vrtDataRecordExecuteNew(this._hSql) && this.DataRecordExecuteNewCustomFields(this._hSql);
            }
            return true;
        }

        public SalBoolean DataSourceExecuteSqlUpdate(IEntity Entity)
        {
            if (Entity.Status == RecordStatus.Edit)
            {
                // return DataRecordExecuteModify(Entity);//base.SendMessageToChildrenOk(4096, 17504, 1, 0) && this.vrtDataRecordExecuteModify(this._hSql) && this.DataRecordExecuteModifyCustomFields(this._hSql);
                return vrtDataRecordExecuteModify(Entity);
            }
            return true;
        }
        public SalBoolean DataSourceExecuteSqlDelete(IEntity Entity)
        {
            if (Entity.Status == RecordStatus.Delete)
            {
                //return DataRecordExecuteRemove(Entity);
                return vrtDataRecordExecuteRemove(Entity);
                //return this.vrtDataRecordExecuteRemove(this._hSql);
            }
            return true;
        }
        public SalBoolean Committed { get; set; }
        public SalBoolean DataSourceExecuteMarkCommitted()
        {
            Committed = true;
            this.i_nRecordState = 0;
            return true;
        }
        public static SalBoolean NeedRollback { get; set; }
        public SalBoolean DataSourceExecuteMarkRollback()
        {
            // base.SendMessageToChildren(64, 17506, 1, 0);
            //this.__lsObjversion = this.__lsOldObjversion;
            //this.i_nRecordState = 0;
            Committed = false;
            NeedRollback = true;
            return true;
        }
        public SalBoolean DataRecordCheckNew(IEntity entity)
        {
            if (!AppEnvironment.IsCheck)
                return true;
            if (!(this.p_sPackageName != ""))
            {
                return true;
            }
            if (this.p_sPackageName.ToString().IndexOf('(') >= 0)
                return true;
            this.__lsAttr = entity.GetAttr(false);
            this._cSessionManager._SetAction("CHECK");
            if (_cSessionManager.DbSQLBlock(entity, this.p_sPackageName + ".NEW__( :info_, :objid_, :objversion_, :attr_, :action_ )"))
            {
                entity.EditAttr("");
                Entity.EditDiffAttr("");
                return _cSessionManager.HandleSqlWarnings(this.__lsResult);
            }
            else
            {
                entity.EditAttr("");
                Entity.EditDiffAttr("");
                return _cSessionManager.StoreSqlResult(_cSessionManager.ErrorMsg);
            }
            //return false;
        }
        public SalBoolean DataRecordCheckModify(IEntity entity)
        {
            if (!AppEnvironment.IsCheck)
                return true;
            if (!(this.p_sPackageName != ""))
            {
                return true;
            }
            //this.DataRecordFetchEdited(ref this.__lsAttr);
            // if (DataSourceBind.OldEntitys.Count  == 0)
            //     return false;
            // IEntity en = DataSourceBind.OldEntitys.Find( p => p["objid"].ToString () == entity["objid"].ToString());
            // if(en == null)
            //    return false;
            if (this.p_sPackageName.ToString().IndexOf('(') >= 0)
                return true;
            this.__lsAttr = entity.GetDiffAttr(false);
            _cSessionManager._SetAction("CHECK");
            if (_cSessionManager.DbSQLBlock(entity, this.p_sPackageName + ".MODIFY__( :info_, :objid_, :objversion_, :attr_, :action_ )"))
            {
                entity.EditAttr("");
                Entity.EditDiffAttr("");
                return _cSessionManager.HandleSqlWarnings(this.__lsResult);
            }
            else
            {
                entity.EditAttr("");
                Entity.EditDiffAttr("");
                return _cSessionManager.StoreSqlResult(_cSessionManager.ErrorMsg);
            }
            //return false;
        }
        public SalBoolean DataRecordCheckRemove(IEntity entity)
        {
            if (!AppEnvironment.IsCheck)
                return true;
            if (!(this.p_sPackageName != ""))
            {
                return true;
            }
            SalString sSql = this.p_sPackageName + ".REMOVE__( :info_, :objid_, :objversion_, :action_ )";
            if (cSessionManager.IsRemote)
            {
                cSessionManager.PrepareSQLBlock(Entity, sSql, true, true, null, System.Data.CommandType.StoredProcedure, 0, "CHECK");
                DataContract.ExecuteResult result = cSessionManager.BatchExecute();
                if (result != null && result.Success)
                {
                    return true;
                }
                else
                {
                    cSessionManager.translate.ShowMessage(cSessionManager.GetSqlLastError(), "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return false;
                }
            }
            else
            {
                cSessionManager.DbTransactionBegin();
                _cSessionManager._SetAction("CHECK");
                if (_cSessionManager.DbSQLBlock(entity, sSql))
                {
                    cSessionManager.DbTransactionClear();

                    return _cSessionManager.HandleSqlWarnings(this.__lsResult);
                }
                else
                {
                    cSessionManager.DbTransactionClear();
                    return _cSessionManager.StoreSqlResult(_cSessionManager.ErrorMsg);
                }
            }
        }
        public SalBoolean DataRecordCheckRequired()
        {
            SalNumber x = 0;
            x = 0;

            return true;
        }
        public SalBoolean FndBinaryObjectNew(IEntity entity, int RefKey = 0)
        {
            if (DataSourceBind.Columns == null || DataSourceBind.Columns.Count == 0)
                return true;
            SalString sSql = "FndBinaryObject_NewObject(:blob_id_,:file_path_,:file_name_,:display_text_,:data_,:binary_object_type_)";
            SalBoolean res = true;
            foreach (DataItem di in DataSourceBind.Columns)
            {
                if (di.IsImage || di.IsRichEdit || di.IsBlobEdit)
                {
                    Fcdbas.Biz.fnd_binary_object binObject = new Biz.fnd_binary_object();
                    if (di.IsImage || di.IsRichEdit)
                    {
                        binObject.data = (byte[])entity[di.ColumnName];
                        binObject.blob_id = 0;
                        binObject.file_path = DataSourceBind.ParentForm.GetPictureFilePath(di.ColumnName);
                        if (!string.IsNullOrEmpty(binObject.file_path))
                            binObject.file_name = Fcdbas.Core.Util.FileHelper.GetFileNameNoPath(binObject.file_path);
                        if (di.IsImage)
                            binObject.binary_object_type = "P";
                        else
                            binObject.binary_object_type = "D";
                    }
                    if (di.IsBlobEdit)
                    {
                        binObject.data = (byte[])entity[di.ColumnName + "_db"];
                        binObject.blob_id = 0;
                        binObject.binary_object_type = "D";
                    }
                    if (cSessionManager.IsRemote)
                    {
                        Dictionary<string, string> dic = new Dictionary<string, string>();
                        if (di.IsImage || di.IsRichEdit)
                            dic.Add(di.ColumnName.Substring(0, di.ColumnName.Length - 3), "blob_id");
                        else
                            dic.Add(di.ColumnName, "blob_id");
                        res = cSessionManager.PrepareSQLBlock(binObject, sSql, false, true, dic, System.Data.CommandType.StoredProcedure, RefKey);

                    }
                    else
                    {
                        res = _cSessionManager.DbSQLBlock(binObject, sSql, false);
                    }
                    if (res)
                    {
                        if (di.IsImage || di.IsRichEdit)
                            entity[di.ColumnName.Substring(0, di.ColumnName.Length - 3)] = binObject.blob_id;
                        else
                            entity[di.ColumnName] = binObject.blob_id;
                    }
                    if (!res)
                        break;
                }
            }
            return res;
        }
        public SalBoolean FndBinaryObjectModify(IEntity entity, int RefKey = 0)
        {
            if (DataSourceBind.Columns == null || DataSourceBind.Columns.Count == 0)
                return true;
            SalString sModifySql = "FndBinaryObject_ModifyObject(:blob_id_,:file_path_,:file_name_,:display_text_,:data_)";
            SalString sNewSql = "FndBinaryObject_NewObject(:blob_id_,:file_path_,:file_name_,:display_text_,:data_,:binary_object_type_)";
            SalString sRemoveSql = "FndBinaryObject_RemoveObject(:blob_id_)";
            SalBoolean res = true;
            foreach (DataItem di in DataSourceBind.Columns)
            {
                if (((di.IsImage || di.IsRichEdit) && DataSourceBind.ParentForm.GetPictureEditing(di.ColumnName)) || di.IsBlobEdit)
                {
                    Fcdbas.Biz.fnd_binary_object binObject = new Biz.fnd_binary_object();
                    int blob_id;
                    if (di.IsImage || di.IsRichEdit)
                    {
                        string idCol = di.ColumnName.Substring(0, di.ColumnName.Length - 3);
                        blob_id = entity[idCol] == null ? 0 : Convert.ToInt32(entity[idCol]);

                        binObject.data = (byte[])entity[di.ColumnName];
                        if (di.IsImage)
                            binObject.binary_object_type = "P";
                        else
                            binObject.binary_object_type = "D";
                    }
                    else
                    {
                        blob_id = entity[di.ColumnName] == null ? 0 : Convert.ToInt32(entity[di.ColumnName]);

                        binObject.data = (byte[])entity[di.ColumnName + "_db"];
                        binObject.binary_object_type = "D";
                    }
                    binObject.blob_id = blob_id;
                    //已经有数据
                    if (blob_id > 0)
                    {
                        ///修改
                        if (binObject.data != null)
                        {
                            if (di.IsImage || di.IsRichEdit)
                            {
                                binObject.file_path = DataSourceBind.ParentForm.GetPictureFilePath(di.ColumnName);
                                if (!string.IsNullOrEmpty(binObject.file_path))
                                    binObject.file_name = Fcdbas.Core.Util.FileHelper.GetFileNameNoPath(binObject.file_path);
                            }
                            if (cSessionManager.IsRemote)
                            {
                                cSessionManager.PrepareSQLBlock(binObject, sModifySql, false);
                                res = true;
                            }
                            else
                            {
                                res = _cSessionManager.DbSQLBlock(binObject, sModifySql, false);
                            }
                        }
                        else//删除
                        {
                            if (cSessionManager.IsRemote)
                            {
                                res = true;
                                cSessionManager.PrepareSQLBlock(binObject, sRemoveSql, false);
                            }
                            else
                            {

                                res = _cSessionManager.DbSQLBlock(binObject, sRemoveSql, false);
                            }
                        }

                    }
                    else//新建
                    {
                        if (binObject.data != null)
                        {
                            if (di.IsImage || di.IsRichEdit)
                            {
                                binObject.file_path = DataSourceBind.ParentForm.GetPictureFilePath(di.ColumnName);
                                if (!string.IsNullOrEmpty(binObject.file_path))
                                    binObject.file_name = Fcdbas.Core.Util.FileHelper.GetFileNameNoPath(binObject.file_path);
                            }
                            if (cSessionManager.IsRemote)
                            {
                                Dictionary<string, string> dic = new Dictionary<string, string>();
                                if (di.IsImage || di.IsRichEdit)
                                    dic.Add(di.ColumnName.Substring(0, di.ColumnName.Length - 3), "blob_id");
                                else
                                    dic.Add(di.ColumnName, "blob_id");
                                res = cSessionManager.PrepareSQLBlock(binObject, sNewSql, false, true, dic, System.Data.CommandType.StoredProcedure, RefKey);
                            }
                            else
                            {
                                res = _cSessionManager.DbSQLBlock(binObject, sNewSql, false);
                            }
                            if (res)
                            {
                                if (di.IsImage || di.IsRichEdit)
                                    entity[di.ColumnName.Substring(0, di.ColumnName.Length - 3)] = binObject.blob_id;
                                else
                                    entity[di.ColumnName] = binObject.blob_id;
                            }
                        }
                    }
                    if (!res)
                        break;
                }
            }
            return res;
        }
        public SalBoolean FndBinaryObjectRemove(IEntity entity)
        {
            if (DataSourceBind.Columns == null || DataSourceBind.Columns.Count == 0)
                return true;
            SalString sSql = "FndBinaryObject_RemoveObject(:blob_id_)";
            SalBoolean res = true;
            foreach (DataItem di in DataSourceBind.Columns)
            {
                if (di.IsImage || di.IsRichEdit || di.IsBlobEdit)
                {
                    Fcdbas.Biz.fnd_binary_object binObject = new Biz.fnd_binary_object();
                    if (di.IsImage || di.IsRichEdit)
                        binObject.blob_id = entity[di.ColumnName.Substring(0, di.ColumnName.Length - 3)] == null ? 0 : Convert.ToInt32(entity[di.ColumnName.Substring(0, di.ColumnName.Length - 3)]);
                    else
                        binObject.blob_id = entity[di.ColumnName] == null ? 0 : Convert.ToInt32(entity[di.ColumnName]);
                    if (cSessionManager.IsRemote)
                    {
                        cSessionManager.PrepareSQLBlock(entity, sSql, false);
                        res = true;
                    }
                    else
                    {
                        res = _cSessionManager.DbSQLBlock(binObject, sSql, false);
                    }
                    if (!res)
                        break;
                }
            }
            return res;
        }
        public SalBoolean DataRecordExecuteNew(IEntity entity)
        {
            if (!(this.p_sPackageName != ""))
            {
                return true;
            }

            SalString sSql = this.p_sPackageName + ".NEW__( :info_, :objid_, :objversion_, :attr_, :action_ )";
            if (cSessionManager.IsRemote)
            {

                int key = cSessionManager.PrepareSQLBlock(entity, sSql, true);
                FndBinaryObjectNew(entity, key);
                return true;
            }
            else
            {
                if (!FndBinaryObjectNew(entity))
                    return false;

                bool standard = true;
                if (this.p_sPackageName.ToString().IndexOf('(') >= 0)
                {
                    sSql = this.p_sPackageName;
                    standard = false;
                }
                else
                {
                    this.__lsAttr = entity.GetAttr(false);
                    _cSessionManager._SetAction("DO");
                }
                if (!_cSessionManager.DbSQLBlock(entity, sSql, standard))
                {

                    entity.EditAttr("");
                    entity.EditDiffAttr("");
                    _cSessionManager.StoreSqlResult(_cSessionManager.ErrorMsg);
                    return false;
                }
                //entity.Status = RecordStatus.Normal;
                if (_cSessionManager.StoreSqlResult(this.__lsResult))
                {
                    // this.__lsAttr = entity.GetAttr();
                    entity.EditAttr("");
                    entity.EditDiffAttr("");
                    this.DataRecordToForm(entity, this.__lsAttr, false);
                    return true;
                }
                return false;
            }
        }
        public SalBoolean DataRecordExecuteModify(IEntity entity)
        {
            if (!(this.p_sPackageName != ""))
            {
                return true;
            }
            // if (DataSourceBind.OldEntitys.Count == 0)
            //    return false;
            //  IEntity en = DataSourceBind.OldEntitys.Find(p => p["objid"].ToString() == entity["objid"].ToString());
            // if (en == null)
            //     return true;
            SalString sSql = this.p_sPackageName + ".MODIFY__( :info_, :objid_, :objversion_, :attr_, :action_ )";
            if (cSessionManager.IsRemote)
            {
                int key = cSessionManager.PrepareSQLBlock(entity, sSql, true);
                FndBinaryObjectModify(entity, key);
                return true;
            }
            else
            {
                if (!FndBinaryObjectModify(entity))
                    return false;
                bool standard = true;
                if (this.p_sPackageName.ToString().IndexOf('(') >= 0)
                {
                    sSql = this.p_sPackageName;
                    standard = false;
                }
                else
                {
                    this.__lsAttr = entity.GetDiffAttr(false);

                    _cSessionManager._SetAction("DO");
                }
                if (!_cSessionManager.DbSQLBlock(entity, sSql, standard))
                {
                    entity.EditAttr("");
                    entity.EditDiffAttr("");
                    _cSessionManager.StoreSqlResult(_cSessionManager.ErrorMsg);
                    return false;
                }
                //entity.Status = RecordStatus.Normal;
                if (_cSessionManager.StoreSqlResult(this.__lsResult))
                {
                    // this.__lsAttr = entity.GetAttr();
                    entity.EditAttr("");
                    entity.EditDiffAttr("");
                    //this.DataRecordToForm(base.__lsAttr, false);
                    entity.SetAttr(this.__lsAttr, true);
                    return true;
                }
                return false;
            }
        }
        public SalBoolean DataRecordExecuteRemove(IEntity entity)
        {
            if (!(this.p_sPackageName != ""))
            {
                return true;
            }
            SalString sSql = this.p_sPackageName + ".REMOVE__( :info_, :objid_, :objversion_, :action_ )";
            if (cSessionManager.IsRemote)
            {
                FndBinaryObjectRemove(entity);
                cSessionManager.PrepareSQLBlock(entity, sSql);
                return true;
            }
            else
            {
                if (!FndBinaryObjectRemove(entity))
                    return false;
                this.__lsAttr = entity.GetAttr(true);
                _cSessionManager._SetAction("DO");
                if (_cSessionManager.DbSQLBlock(entity, sSql))
                {
                    entity.EditAttr("");
                    entity.EditDiffAttr("");

                }
                return _cSessionManager.StoreSqlResult(_cSessionManager.ErrorMsg);
            }
        }
        public SalBoolean DataRecordExecuteStateEvent(IEntity entity, SalString sEvent)
        {
            if (!(this.p_sPackageName != ""))
            {
                return true;
            }
            this.__lsAttr = "";
            _cSessionManager._SetAction("CHECK");
            if (!_cSessionManager.DbSQLBlock(entity, this.p_sPackageName + "." + Int.ClientNameToDbName(sEvent) + "__( :info_, :objid_, :objversion_, :attr_, :action_ )"))
            {
                return false;
            }
            if (!_cSessionManager.HandleSqlWarnings(this.__lsResult))
            {
                return false;
            }
            _cSessionManager._SetAction("DO");
            if (!_cSessionManager.DbSQLBlock(entity, this.p_sPackageName + "." + Int.ClientNameToDbName(sEvent) + "__( :info_, :objid_, :objversion_, :attr_, :action_ )"))
            {
                return false;
            }
            if (_cSessionManager.StoreSqlResult(this.__lsResult))
            {
                // this.DataRecordToForm(base.__lsAttr, false);
                entity.SetAttr(this.__lsAttr, true);
                return true;
            }
            return false;
        }



        /// <summary>
        /// 未实现
        /// </summary>
        /// <param name="bDirty"></param>
        /// <returns></returns>
        public SalNumber DataSourceDetailModified(SalBoolean bDirty)
        {

            return 0;
        }
        /// <summary>
        /// 未实现
        /// </summary>
        /// <returns></returns>
        public SalNumber DataSourceFieldModified()
        {
            if (RecordRefresh)
                return 0;
            this.Committed = false;
            NeedRollback = false;
            this.i_nRecordState = 4;
            return 0;
        }

        public SalNumber __ActionStrToNumber(SalString sAction)
        {
            if (sAction == "")
            {
                return SalNumber.Null;
            }
            if (sAction == "populate")
            {
                return 1;
            }
            if (sAction == "querydialog")
            {
                return 2;
            }
            if (sAction == "new")
            {
                return 3;
            }
            if (sAction == "template")
            {
                return 4;
            }
            if (sAction == "draft")
            {
                return 5;
            }
            if (sAction == "search")
            {
                return 6;
            }
            return SalNumber.Null;
        }
        internal void SaveTemplate()
        {
            //FndDataRow currentDataRow = this.GetCurrentDataRow();
            //if (currentDataRow != null)
            //{
            //    FndDataTable fndDataTable = new FndDataTable(base.Meta);
            //    fndDataTable.Rows.Add(currentDataRow);
            //    IFndDocumentCollectionContainer fndDocumentCollectionContainer = Int.Explorer.NavigationNotification.ActiveFeature as IFndDocumentCollectionContainer;
            //    if (fndDocumentCollectionContainer != null)
            //    {
            //        string title = fndDocumentCollectionContainer.Title;
            //        ITemplateManagerService templateManagerService = (ITemplateManagerService)Int.Explorer.Services.GetServiceProvider(typeof(ITemplateManagerService));
            //        templateManagerService.SaveTemplate(Int.Explorer, fndDataTable, base.Meta.Entity, title, this.Name);
            //    }
            //}
        }
        public SalBoolean __ActionApplyTemplate(SalNumber nAction, SalString sID)
        {

            return false;
        }

        public SalNumber RecordStateGet()
        {
            return this.i_nRecordState;
        }
        public SalNumber SourceFlagsGet()
        {
            return this.p_nSourceFlags;
        }
        public SalBoolean SourceFlagsSet(SalNumber nFlags, SalBoolean bSet)
        {
            if (bSet)
            {
                this.p_nSourceFlags |= nFlags;
            }
            else
            {
                if (this.SourceFlagsQuery(nFlags))
                {
                    this.p_nSourceFlags -= nFlags;
                }
            }
            return true;
        }
        public SalBoolean SourceFlagsQuery(SalNumber nFlags)
        {
            return (this.p_nSourceFlags & nFlags) == nFlags;
        }
        public SalNumber SourceStateGet()
        {
            return this.i_nDataSourceState;
        }
        public SalBoolean SourceStateQuery(SalNumber nState)
        {
            return this.i_nDataSourceState == nState;
        }



        public static implicit operator cSessionManager(cDataSource self)
        {
            return self._cSessionManager;
        }

        public static implicit operator cDataSource(cSessionManager super)
        {
            return (cDataSource)super._derived;
        }


        public virtual SalBoolean vrtDataRecordCheckModify(IEntity entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordCheckModify(entity);
            }
            return lateBind.vrtDataRecordCheckModify(entity);
        }
        public virtual SalBoolean vrtDataRecordCheckNew(IEntity entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordCheckNew(entity);
            }
            return lateBind.vrtDataRecordCheckNew(entity);
        }
        public virtual SalBoolean vrtDataRecordCheckRemove(IEntity entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordCheckRemove(entity);
            }
            return lateBind.vrtDataRecordCheckRemove(entity);
        }
        public virtual SalBoolean vrtDataRecordCheckRequired()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordCheckRequired();
            }
            return lateBind.vrtDataRecordCheckRequired();
        }
        public virtual SalNumber vrtDataRecordCopy(SalNumber nWhat)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordCopy(nWhat);
            }
            return lateBind.vrtDataRecordCopy(nWhat);
        }
        public virtual SalNumber vrtDataRecordDuplicate(SalNumber nWhat)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordDuplicate(nWhat);
            }
            return lateBind.vrtDataRecordDuplicate(nWhat);
        }
        public virtual SalBoolean vrtDataRecordExecuteModify(IEntity entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordExecuteModify(entity);
            }
            return lateBind.vrtDataRecordExecuteModify(entity);
        }
        public virtual SalBoolean vrtDataRecordExecuteNew(IEntity entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordExecuteNew(entity);
            }
            return lateBind.vrtDataRecordExecuteNew(entity);
        }

        public virtual SalBoolean vrtDataRecordExecuteRemove(IEntity entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordExecuteRemove(entity);
            }
            return lateBind.vrtDataRecordExecuteRemove(entity);
        }
        public virtual SalBoolean vrtDataRecordExecuteStateEvent(IEntity entity, SalString sEvent)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordExecuteStateEvent(entity, sEvent);
            }
            return lateBind.vrtDataRecordExecuteStateEvent(entity, sEvent);
        }
        public virtual SalNumber vrtDataRecordFetchEditedUser(ref SalString lsAttr)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordFetchEditedUser(ref lsAttr);
            }
            return lateBind.vrtDataRecordFetchEditedUser(ref lsAttr);
        }
        public SalNumber DataRecordFetchEditedUser(ref SalString lsAttr)
        {
            return 0;
        }
        public virtual SalBoolean vrtDataRecordGetDefaults()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordGetDefaults();
            }
            return lateBind.vrtDataRecordGetDefaults();
        }
        private string LogicalObject
        {
            get { return this.p_sModule + "." + this.p_sLogicalUnit + "." + this.p_sViewName; }
        }
        private void CreateEntity()
        {
            if (this.LogicalObject == "..")
                return;
            DataSourceBinding dsb = this._derived as DataSourceBinding;
            if (dsb.DataSource == null || !(dsb.DataSource is IEntityList))
            {
                IEntityList list = CreateEntityList();
                list.Add(dsb.CreateEntity());
                dsb.DataSource = list;
                dsb.Position = 0;
                dsb.ResumeFormBinding();
            }
            else
                dsb.AddNew();
            if (dsb.LogicalParent != null)
            {
                IEntity parent = dsb.LogicalParent.Current as IEntity;
                if (dsb.Columns == null)
                    return;
                ///根据parent key 自动给值
                foreach (DataItem di in dsb.Columns)
                {
                    if (di.Flags == Platform.Enums.DataItemFlags.ParentKey)
                    {
                        IEntity dtl = dsb.Current as IEntity;
                        dtl[di.ColumnName] = parent[di.ColumnName];
                    }
                }

            }
            IEntity en = dsb.Current as IEntity;
            if (en != null)
            {
                en.Status = RecordStatus.New;
                en["objid"] = "null";
            }
            // dsb.ResetBindings(false);
            //Type t = Fcdbas.Core.Reflection.TypeX.GetType(this.LogicalObject);
            //if (t == null)
            //    throw new Exception("没有找到指定的逻辑单元");
            //object obj = Fcdbas.Core.Reflection.TypeX.CreateInstance(t);
            //if (obj is IEntity)
            //{
            //    IEntity Entity = obj as IEntity;
            //    DataSourceBinding dsb = this._derived as DataSourceBinding;
            //    List<IEntity> list = dsb.DataSource as List<IEntity>;
            //    list.Add(Entity);
            //    dsb.AddNew();
            //    // this.DataSource = obj;
            //}
            //else
            //    throw new Exception("逻辑单元必须从Entitiy继承");
        }
        public SalNumber QuickAddNew()
        {
            i_nRecordState = 8;
            CreateEntity();
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordNew(2);
            }
            return lateBind.vrtDataRecordNew(2);

        }
        public virtual SalNumber vrtDataRecordNew(SalNumber nWhat)
        {
            i_nRecordState = 8;
            CreateEntity();
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordNew(nWhat);
            }
            return lateBind.vrtDataRecordNew(nWhat);
        }

        public virtual SalNumber vrtDataRecordPaste(SalNumber nWhat)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordPaste(nWhat);
            }
            return lateBind.vrtDataRecordPaste(nWhat);
        }
        public virtual SalBoolean vrtDataRecordPrepareNew(IEntity entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordPrepareNew(entity);
            }
            return lateBind.vrtDataRecordPrepareNew(entity);
        }
        public virtual SalNumber DataRecordRemoveMulti(List<IEntity> enList)
        {
            int dels = 0;
            IEntityList list = this.DataSourceBind.DataSource as IEntityList;
            if (list == null)
                return 0;
            for (int i = 0; i < enList.Count; i++)
            {
                IEntity entity = enList[i];
                if (entity.Status == RecordStatus.Delete || entity.Status == RecordStatus.New)
                {
                    if (entity.Status == RecordStatus.New)
                    {
                        list.Remove(entity);
                    }
                    else
                    {
                        entity.Status = RecordStatus.Normal;
                    }
                }
                else
                {
                    if (this.vrtDataRecordCheckRemove(entity))
                    {
                        this.Committed = false;
                        i_nRecordState = 32;
                        entity.Status = RecordStatus.Delete;

                        dels++;
                    }
                }
            }


            if (list != null)
            {
                bool isDel = false;
                foreach (IEntity en in list)
                {
                    if (en.Status == RecordStatus.Delete)
                    {
                        isDel = true;
                        i_nRecordState = 32;
                        break;
                    }
                    if (en.Status == RecordStatus.New)
                    {
                        isDel = true;
                        i_nRecordState = 8;
                        break;
                    }
                    if (en.Status == RecordStatus.Edit)
                    {
                        isDel = true;
                        i_nRecordState = 4;
                        break;
                    }

                }
                if (isDel)
                {
                    this.Committed = false;

                }
                else
                {
                    i_nRecordState = 0;
                    this.Committed = true;
                }
            }
            else
            {
                i_nRecordState = 0;
                this.Committed = true;
            }
            if (dels > 0)
            {
                if (DataSourceDeleted != null)
                    DataSourceDeleted(this, new ReturnEventArgsSalBoolean());
            }
            return dels;
        }
        public virtual SalNumber vrtDataRecordRemove(IEntity entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordRemove(entity);
            }
            return lateBind.vrtDataRecordRemove(entity);
        }


        public virtual SalNumber vrtDataRecordToFormUser(ref SalString lsServerAttr, ref SalBoolean bMarkAsEdited)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordToFormUser(ref lsServerAttr, ref bMarkAsEdited);
            }
            return lateBind.vrtDataRecordToFormUser(ref lsServerAttr, ref bMarkAsEdited);
        }
        public SalNumber DataRecordToFormUser(ref SalString lsServerAttr, ref SalBoolean bMarkAsEdited)
        {
            return 0;
        }
        public virtual SalBoolean vrtDataRecordValidate()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataRecordValidate();
            }
            return lateBind.vrtDataRecordValidate();
        }
        public SalBoolean DataRecordValidate()
        {
            if (!(this.i_nRecordState & 32))
            {
                return this.vrtDataRecordCheckRequired();
            }
            return true;
        }
        public virtual SalBoolean vrtDataSourceCheck(IEntity Entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceCheck(Entity);
            }
            return lateBind.vrtDataSourceCheck(Entity);
        }
        public virtual SalNumber vrtDataSourceClear(SalNumber nWhat, SalNumber nParam)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceClear(nWhat, nParam);
            }
            return lateBind.vrtDataSourceClear(nWhat, nParam);
        }

        /// <summary>
        /// 未实现
        /// </summary>
        /// <param name="nParam"></param>
        /// <returns></returns>
        public SalBoolean DataSourceClearIt(SalNumber nParam)
        {

            return true;
        }
        public virtual SalNumber vrtDataSourceDetailModified(SalBoolean bDirty)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceDetailModified(bDirty);
            }
            return lateBind.vrtDataSourceDetailModified(bDirty);
        }
        public virtual SalBoolean vrtDataSourceExecuteMarkCommitted()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceExecuteMarkCommitted();
            }
            return lateBind.vrtDataSourceExecuteMarkCommitted();
        }
        public virtual SalBoolean vrtDataSourceExecuteMarkRollback()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceExecuteMarkRollback();
            }
            return lateBind.vrtDataSourceExecuteMarkRollback();
        }
        public virtual SalBoolean vrtDataSourceExecutePrepareRollback()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceExecutePrepareRollback();
            }
            return lateBind.vrtDataSourceExecutePrepareRollback();
        }

        public virtual SalBoolean vrtDataSourceExecuteSqlDelete(IEntity Entitiy)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceExecuteSqlDelete(Entitiy);
            }
            return lateBind.vrtDataSourceExecuteSqlDelete(Entitiy);
        }
        public virtual SalBoolean vrtDataSourceExecuteSqlInsert(IEntity Entitiy)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceExecuteSqlInsert(Entitiy);
            }
            return lateBind.vrtDataSourceExecuteSqlInsert(Entitiy);
        }





        public virtual SalBoolean vrtDataSourceExecuteSqlUpdate(IEntity Entitiy)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceExecuteSqlUpdate(Entitiy);
            }
            return lateBind.vrtDataSourceExecuteSqlUpdate(Entitiy);
        }
        public virtual SalNumber vrtDataSourceFieldModified()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceFieldModified();
            }
            return lateBind.vrtDataSourceFieldModified();
        }

        public virtual SalNumber vrtDataSourceMarkRollback()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceMarkRollback();
            }
            return lateBind.vrtDataSourceMarkRollback();
        }
        public virtual SalNumber vrtDataSourcePopulate(SalNumber nWhat, SalNumber nParam)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourcePopulate(nWhat, nParam);
            }
            return lateBind.vrtDataSourcePopulate(nWhat, nParam);
        }
        /// <summary>
        /// 未实现
        /// </summary>
        /// <param name="nWhat"></param>
        /// <param name="nParam"></param>
        /// <returns></returns>
        public SalNumber DataSourcePopulate(SalNumber nWhat, SalNumber nParam)
        {
            return 1;
        }


        public virtual SalNumber vrtDataSourcePrepareRollback()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourcePrepareRollback();
            }
            return lateBind.vrtDataSourcePrepareRollback();
        }

        public virtual SalNumber vrtDataSourceRefresh(DataSourcePopulate nWhat)
        {
            // CreateEntity();
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceRefresh(nWhat);
            }
            return lateBind.vrtDataSourceRefresh(nWhat);
        }
        public virtual SalString vrtDataSourceUserWhereGet()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceUserWhereGet();
            }
            return lateBind.vrtDataSourceUserWhereGet();
        }
        public virtual SalString vrtDataSourceUserFieldGet()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceUserFieldGet();
            }
            return lateBind.vrtDataSourceUserFieldGet();
        }
        public SalString DataSourceUserFieldGet()
        {
            return "";
        }
        public SalString DataSourceUserWhereGet()
        {
            //return this.__lsLastUserWhere;
            return p_lsDefaultWhere;
        }
        public virtual SalString vrtDataSourceUserOrderByGet()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceUserOrderByGet();
            }
            return lateBind.vrtDataSourceUserOrderByGet();
        }
        public SalString DataSourceUserOrderByGet()
        {
            return this.__lsLastUserOrderBy;
        }
        private int _RecordCount;
        public int RecordCount
        {
            get
            {
                return _RecordCount;
            }

        }
        private int _RecordPos = 0;
        public int RecordPos
        {
            get
            {
                return _RecordPos;
            }
        }
       
        private void GetQueryCondition(ref SalString where, ref SalString orderby, ref SalString fields)
        {
            fields = this.vrtDataSourceUserFieldGet();

            where = this.vrtDataSourceUserWhereGet();
            string parentWhere = this.i_lsParentWhere;
            if (string.IsNullOrEmpty(parentWhere))
                parentWhere = this.DataSourceBind.ParentWhere;
            if (where.IsEmpty)
            {
                where = parentWhere;
                
            }
            else
            {
                if (!string.IsNullOrEmpty(parentWhere))
                {

                    where += " and " + parentWhere;
                }
            }
            if (where.IsEmpty)
                where = this.i_lsUserWhere;
            else
            {
                if (!this.i_lsUserWhere.IsEmpty)
                    where = where + " and " + this.i_lsUserWhere;
            }
            orderby = this.vrtDataSourceUserOrderByGet();
            if (orderby.IsEmpty)
                orderby = this.i_sUserOrderBy;
            else
            {
                if (!this.i_sUserOrderBy.IsEmpty)
                    orderby = orderby + " , " + this.i_sUserOrderBy;
            }
            string dbWhere = FndVpdPolicy_GetPolicy(this.p_sViewName);
            if (!string.IsNullOrEmpty(dbWhere))
            {
                if (where.IsEmpty)
                    where = dbWhere;
                else
                    where = dbWhere + " and " + where;
            }
        }
        public void PrepareQuery()
        {
            SalString where = "";
            SalString orderby = "";
            SalString fields = "";
            this.i_nRecordState = 0;
            GetQueryCondition(ref where, ref orderby, ref fields);
            cSessionManager.PrepareQueryBlock(DataSourceBind.Name, DataSourceBind.ObjectUnit, where, orderby, fields, 0, 0);
        }
        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="nWhat">3重新抽取,1抽取下一段,2抽取所有,4刷新当前记录</param>
        /// <returns></returns>
        public SalNumber DataSourceRefresh(SalNumber nWhat)
        {
            return DataSourceRefresh(nWhat, this.Entity);
        }
        private SalNumber DataSourceRefresh(SalNumber nWhat, IEntity Entity)
        {
            SalNumber x = 0;
            SalNumber x2 = 0;
            SalString where = "";
            SalString orderby = "";
            SalString fields = "";
            this.i_nRecordState = 0;
            GetQueryCondition(ref where, ref orderby, ref fields);
            if (nWhat == 3 && this.DataSourceBind.IsLimit)
            {
                _RecordCount = _cSessionManager.FindCount(Entity, where);

            }
            if (cSessionManager.IsRemote)
            {
                __DataSource = _cSessionManager.FindFromRemote(Entity, where, fields, orderby, nWhat, this.DataSourceBind.RecordLimit);
            }
            else
            {
                __DataSource = _cSessionManager.Find(Entity, where, fields, orderby, nWhat, this.DataSourceBind.RecordLimit);
            }

            _RecordPos = _cSessionManager.RecordPos;
            if (__DataSource != null)
            {
                //this.DataSourceBind.OnFetchRowsDone(this, __DataSource.Count);
                return __DataSource.Count;
            }
            else
            {
                //this.DataSourceBind.OnFetchRowsDone(this, 0);
                return 0;
            }
        }
        internal SalNumber DataRecordRefreshEntity(IEntity entity)
        {
            return DataSourceRefresh(4, entity);
        }
        public string FndVpdPolicy_GetPolicy(string viewName)
        {
            //SalString select = "select FndVpdPolicy_GetPolicy('" + viewName + "');";
            //return cSessionManager.GetUserFunctionValue<string>(select, true);
            if (VpdPolicys == null)
            {
                string conName = Fcdbas.Biz.fnd_vpd_policy.Meta.ConnName;
                Fcdbas.Biz.fnd_vpd_policy.Meta.ConnName = Fcdbas.Biz.FndDAL.SQLite;
                // string where = " actived = 'TRUE' and object_name ='" + viewName + "'";
                VpdPolicys = Fcdbas.Biz.fnd_vpd_policy.FindAll();
                Fcdbas.Biz.fnd_vpd_policy.Meta.ConnName = conName;
            }
            var where = (from p in VpdPolicys //.Select(p => p.actived=="TRUE" && p.object_name==viewName).FirstOrDefault
                         where p.actived == "TRUE" && p.object_name == viewName
                         select p.where_clause).FirstOrDefault();
            //if (list != null && list.Count > 0)
            //{
            //    return list[0].where_clause;
            //}
            return where;
        }
        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="lsUserWhere"></param>
        /// <returns></returns>
        public SalNumber DataSourceRefresh(SalString lsUserWhere, SalString lsOrderBy)
        {

            SalString where = "";
            SalString orderby = lsOrderBy;
            SalString fields = "";
            this.i_nRecordState = 0;
            fields = this.vrtDataSourceUserFieldGet();
          //  p_lsDefaultWhere = lsUserWhere;
           // __lsLastUserOrderBy = lsOrderBy;
            where = lsUserWhere;
            string dbWhere = FndVpdPolicy_GetPolicy(this.p_sViewName);
            if (!string.IsNullOrEmpty(dbWhere))
            {
                if (where.IsEmpty)
                    where = dbWhere;
                else
                    where = dbWhere + " and " + where;
            }
            //orderby = this.vrtDataSourceUserOrderByGet();
            //if (orderby.IsEmpty)
            //    orderby = this.i_sUserOrderBy;
            //else
            //{
            //    if (!this.i_sUserOrderBy.IsEmpty)
            //        orderby = orderby + " , " + this.i_sUserOrderBy;
            //}
            if (cSessionManager.IsRemote)
            {
                __DataSource = _cSessionManager.FindFromRemote(Entity, where, fields, orderby, 2, this.DataSourceBind.RecordLimit);
            }
            else
                __DataSource = _cSessionManager.Find(Entity, where, fields, orderby, 2, this.DataSourceBind.RecordLimit);
            _RecordPos = _cSessionManager.RecordPos;
            if (__DataSource != null)
            {
                // this.DataSourceBind.OnFetchRowsDone(this, __DataSource.Count);
                return __DataSource.Count;
            }
            else
            {
                //this.DataSourceBind.OnFetchRowsDone(this, 0);
                return 0;
            }
        }
        public virtual SalNumber vrtDataSourceSave(SalNumber nWhat)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceSave(nWhat);
            }
            return lateBind.vrtDataSourceSave(nWhat);
        }
        public virtual SalBoolean vrtDataSourceSaveCheck(IEntity Entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceSaveCheck(Entity);
            }
            return lateBind.vrtDataSourceSaveCheck(Entity);
        }


        public virtual SalNumber vrtDataSourceSaveMarkCommitted()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceSaveMarkCommitted();
            }
            return lateBind.vrtDataSourceSaveMarkCommitted();
        }


        public virtual SalBoolean vrtDataSourceSaveRemoved(IEntity Entity)
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceSaveRemoved(Entity);
            }
            return lateBind.vrtDataSourceSaveRemoved(Entity);
        }

        public virtual SalBoolean vrtDataSourceValidate()
        {
            cDataSource.LateBind lateBind = (cDataSource.LateBind)this._derived;
            if (lateBind == null)
            {
                return this.DataSourceValidate();
            }
            return lateBind.vrtDataSourceValidate();
        }



        public DataSourceBinding DataSourceBind
        {
            get
            {
                return this._derived as DataSourceBinding;
            }
        }
        public object Derived
        {
            get { return this._derived; }
        }

        public void AttachMessageActions(ISalWindow target)
        {
            System.Collections.Generic.List<System.Delegate> actionsHandlers = SalWindow.GetActionsHandlers(target);
            foreach (System.Delegate current in actionsHandlers)
            {


                this.WindowActions += (WindowActionsEventHandler)current;

            }
        }

        public void DefaultWndProc(ref System.Windows.Forms.Message m)
        {
            // throw new NotImplementedException();
        }

        public SalWindow GetSalWindowImpl()
        {
            return this.m_salWindow;
        }

        public int GetWindowType()
        {
            return 0;
        }
    }
}
